
var app = getApp();
var MyRequest = require("../../utils/MyRequest.js");
var Global = require("../../Global.js");
var sign = require("../../utils/sign.js");

Page({
  data: {
    Username: '',
    Password: '',
    Code: "",
    LoginType: true,
    time: 60,
    disabled: false,
    giftShow: false,
    cityShow: false,
    citys: [
    ],
    index: 0,
    isNewUser: false,
    cityId: '',
    couponList: []
  },
  onLoad: function () {

  },
  dologin: function (event) {
    var that = this;
    that.Presenter.login(that)
  },
  Presenter: {

    login: function (that) {
      var User;
      if (that.data.LoginType) {
        User = {
          action: "login",
          mobile: that.data.Username,
          password: that.data.Password,
        }
      } else {
        User = {
          action: "quickMobileLogin",
          mobile: that.data.Username,
          code: that.data.Code
        }
        var mysign = sign.getSign(User);
        User.sign = mysign;
      }
      if (that.data.isNewUser) {
        User = {
          action: "quickMobileLogin",
          mobile: that.data.Username,
          code: that.data.Code,
          cityId:that.data.citys[that.data.index].id
        }
        var mysign = sign.getSign(User);
        User.sign = mysign;
      }
      console.log(User);
      MyRequest.MyGetWithToast("Login", that, function (result) {
        //用户信息存进全局
        if (result.data.success) {
          Global.setUserStorage(result.data.data)
          Global.setUserSign(result.data.data.userSign);
          if (result.data.msg == "注册成功") {
            that.getGift(that);
          } else {
            //加入缓存
            wx.switchTab({
              url: '/pages/mine/mine',
              success: function (res) {
                var page = getCurrentPages().pop();
                if (page == undefined || page == null) return;
                page.onShow();
              }

            })
          }

        }
      }, User);
    },
    code: function (that) {

      var Data = {
        action: "quickMobileCode",
        mobile: that.data.Username,
      }

      MyRequest.MyGetWithToast("Code", that, function (result, that) {
        console.log(result);
        if (!result.data.data) {
          that.getCitys(that);
          that.setData({
            cityShow: true,
            isNewUser: true
          })

        }
        that.setData({
          disabled: true
        })

        var t = setInterval(function () {
          that.setData({ time: that.data.time - 1 })
          if (that.data.time <= 0) {
            clearInterval(t);
            that.setData({
              disabled: false,
              time: 60
            })
          }
        }, 1000);


      }, Data);
    },
  },
  Username_input: function (event) {
    this.setData({ Username: event.detail.value })
  },
  Password_input: function (event) {

    this.setData({ Password: event.detail.value })
  },
  Code_input: function (event) {
    this.setData({ Code: event.detail.value })
  },
  toregister: function () {
    wx.navigateTo({
      url: '/pages/register/register'
    })
  },
  toforget: function () {
    wx.navigateTo({
      url: '/pages/forget/forget'
    })
  },
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },
  getCode: function () {

    this.Presenter.code(this);
  },
  onSwitch: function (event) {
    if (event.target.id == "switch-pwd") {
      if (this.data.LoginType) {
        return;
      }
      this.setData({
        LoginType: true
      })

    } else {
      if (!this.data.LoginType) {
        return;
      }
      this.setData({
        LoginType: false
      })
    }
  },
  closeGift: function () {

    //加入缓存
    wx.switchTab({
      url: '/pages/mine/mine',
      success: function (res) {
        var page = getCurrentPages().pop();
        if (page == undefined || page == null) return;
        page.onShow();
      }

    })
    this.setData({
      giftShow: false
    });
  },
  getGift: function (that) {
    var mobile = {
      mobile: this.data.Username
    }
    MyRequest.MyGet("Gift", that, function (result, that) {
      if (result.data.success) {
        //显示出大礼包列表
        console.log(result.data.data);
        that.setData({
          couponList: result.data.data,
          giftShow: true
        });
      } else {
        that.closeGift();
      }
    }, mobile)
  },
  OnCityChange: function (event) {
    var v = event.detail.value;
    this.setData({
      index: event.detail.value
    })
  },
  closeCity: function () {
    this.setData({
      cityShow: false
    });
  },
  getCitys: function (that) {
    var data = {
      action: "list"
    }
    MyRequest.MyGet("Citys", that, function (result, that) {
      that.setData({
        citys: result.data.data,
        cityShow: true
      })
      //绑定数据
    }, data)
  }
}) 